package com.lcq.privacysupport;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.TextUtils;
import android.widget.Toast;

import java.util.Random;

public class Privacy {
    public static final String SP_PRIVACY = "sp_privacy";
    public static final String PRIVACY_AGREE = "privacy_agree";
    private final Activity activity;

    public Privacy(Activity activity) {
        this.activity = activity;
    }

    public void check(final PrivacyCallback callback) {
        //通过SharedPreferences 获取是否已经同意了隐私政策协议
        boolean agree = activity.getSharedPreferences(SP_PRIVACY, Context.MODE_PRIVATE).getBoolean(PRIVACY_AGREE, false);

        if (agree) {//之前已经同意则直接回调onAgree
            callback.onAgree();
            return;
        }

        //接下来就是进行网络请求了，下面是本地构造的数据来模拟请求协议接口，返回数据
        callPrivacyInfo(new ProtocolRequestCallback() {
            @Override
            public void onProtocolClose() {
                activity.getSharedPreferences(SP_PRIVACY, Context.MODE_PRIVATE).edit().putBoolean(PRIVACY_AGREE, true).apply();
                callback.onAgree();
            }

            @Override
            public void onCallbackProtocolResult(final ProtocolResult result) {
                //主线程启动隐私弹窗
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        new PrivacyDialog.Builder()
                                .activity(activity)
                                .protocolResult(result)
                                .callback(callback)
                                .build()
                                .show();
                    }
                });
            }

            @Override
            public void onFail(int code, String msg) {
            }

            @Override
            public String getProtocolType() {
                //返回弹窗首页的类型
                return ProtocolInfo.TYPE_FIRST;
            }
        }, true);
    }

    /**
     * 展示协议界面
     *
     * @param type 协议类型
     */
    public void showProtocol(final String type) {
        callPrivacyInfo(new ProtocolRequestCallback() {
            @Override
            public void onProtocolClose() {
                Toast.makeText(activity, "未打开协议开关", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onCallbackProtocolResult(ProtocolResult result) {
                if (result == null || TextUtils.isEmpty(result.getContent())) {
                    Toast.makeText(activity, "无对应类型的协议:" + type, Toast.LENGTH_LONG).show();
                    return;
                }

                Intent intent = new Intent(activity, ProtocolActivity.class);
                intent.putExtra(ProtocolActivity.EXTRA_TITLE, result.getName());
                intent.putExtra(ProtocolActivity.EXTRA_CONTENT, result.getContent());
                activity.startActivity(intent);
            }

            @Override
            public void onFail(int code, String msg) {
                Toast.makeText(activity, "协议请求失败", Toast.LENGTH_LONG).show();
            }

            @Override
            public String getProtocolType() {
                return type;
            }
        }, false);
    }

    /**
     * 请求协议信息
     *
     * @param callback
     * @param failRetry 请求失败是否重复尝试
     */
    private void callPrivacyInfo(final ProtocolRequestCallback callback, final boolean failRetry) {
        //模拟获取协议信息
        int response = new Random().nextInt(1);//模拟随机生成请求返回结果,随机bound设置为2以上可以模拟其他情况
        switch (response) {
            case 0://模拟成功请求到协议信息
                ProtocolResult protocolResult = ProtocolResult.obtain(callback.getProtocolType());
                callback.onCallbackProtocolResult(protocolResult);
                break;
            case 1://模拟请求协议信息失败
                if (failRetry) {
                    handleFail(callback, "协议请求失败", true);
                } else {
                    callback.onFail(response, "协议请求失败");
                }
                break;
            case 2://模拟请求协议信息成功，但是协议开关关闭
                callback.onProtocolClose();
                break;
            default:
                callback.onFail(response, "未知错误");
        }
    }

    private void handleFail(final ProtocolRequestCallback callback, final String msg, final boolean failRetry) {
        if (failRetry) {
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    new AlertDialog.Builder(activity, android.R.style.Theme_DeviceDefault_Light_Dialog_Alert)
                            .setTitle("请重试")
                            .setMessage(msg)
                            .setCancelable(false)
                            .setNegativeButton("确认", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    callPrivacyInfo(callback, true);
                                }
                            }).show();
                }
            });
        } else {
            callback.onFail(-1, msg);
        }
    }
}
